<?php

namespace App\Http\Controllers\Admin;

use App\Model\Role;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;

class UserController extends Controller
{
    /**
     * 获取用户列表页
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = User::orderBy("user_id","asc")
            ->where(function ($query) use ($request){
                $username = $request->input("username");
                $email = $request->input("email");
                if(!empty($username)){
                    $query->where("user_name","like","%".$username."%");
                }
                if(!empty($email)){
                    $query->where("email","like","%".$email."%");
                }
            })
            ->paginate($request->input('num')?$request->input('num'):5);
        //查询user数据
        //$user = User::paginate(3);

        return view("admin.user.list",compact('user','request'));
    }

    /**
     * 返回用户添加页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("admin.user.add");
    }

    /**
     * 执行添加操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //1.接收前端传回的数据
        $input = $request->all();
        //return $input;

        //2.验证数据  email  username  pass  repass


        //3.将数据存储到user表
        $username = $input['username'];
        $email = $input['email'];
        $pass = Crypt::encrypt($input['pass']);

        $res = User::create(['user_name'=>$username,'user_pass'=>$pass,'email'=>$email]);

        //4.根据是否添加成功，给前端返回一个json反馈
        if($res){
            $data = [
                'status' => 0,
                'message'=> '添加成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '添加失败！'
            ];
        }
        //json_decode();
        return $data;

    }

    /**
     * 显示一条数据
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 返回一个修改页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id查询用户
        $user = User::find($id);
        return view("admin.user.edit",compact('user'));
    }

    /**
     * 执行修改操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //1.根据id获取要修改的记录
        $user = User::find($id);

        //2.获取要修改的值
        $email = $request->input('email');
        $username = $request->input('username');

        $user->user_name=$username;
        $user->email=$email;

        $res = $user->save();

        if($res){
            $data = [
                'status' => 0,
                'message'=> '修改成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '修改失败！'
            ];
        }
        //json_decode();
        return $data;

    }

    /**
     * 执行删除操作
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $user = User::find($id);
        $res = $user->delete();
        \DB::table("user_role")->where("user_id",$id)->delete();

        if($res){
            $data = [
                'status' => 0,
                'message'=> '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '删除失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    //用户多删除方法
    public function delAll(Request $request){
        $input = $request->input("ids");
        $res = User::destroy($input);

        foreach ($input as $id){
            \DB::table("user_role")->where("user_id",$id)->delete();
        }
        if($res){
            $data = [
                'status' => 0,
                'message'=> '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '删除失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    //用户赋予角色方法
    public function actor($id){
        //获取当前用户
        $user = User::find($id);
        //获取所有权限列表
        $role = Role::get();

        //获取当前用户拥有的角色
        $own_roles = $user->role;
        //dd($own_roles);
        //角色拥有的权限id
        $own_role=[];
        foreach ($own_roles as $v){
            $own_role[] = $v->id;
        }
        //dd($own_role);

        return view('admin.user.actor',compact('user','role','own_role'));
    }

    //处理赋予角色的方法
    public function doactor(Request $request){
        //获取角色id
        $input = $request->except("_token");
        //dd($input);

        //删除当前用户拥有的角色
        \DB::table("user_role")->where("user_id",$input['user_id'])->delete();

        //添加新授予的角色
        if(!empty($input['role_id'])){
            foreach ($input['role_id'] as $item) {
                \DB::table("user_role")->insert(['user_id'=>$input['user_id'],'role_id'=>$item]);
            }
        }

        return redirect("admin/user");
    }
}
